﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;

namespace StudentManagementSystem
{
    // 日志类
    public class Log
    {
        public int Id { get; set; } // 日志编号
        public DateTime Time { get; set; } // 日志时间
        public string Content { get; set; } // 日志内容

        // 构造函数，初始化日志信息
        public Log(int id, DateTime time, string content)
        {
            Id = id;
            Time = time;
            Content = content;
        }

        // 显示日志信息
        public void ShowInfo()
        {
            Console.WriteLine($"日志编号：{Id}，日志时间：{Time}，日志内容：{Content}");
        }
    }

    public class LogHelper
    {
        // 添加日志
        public static void AddLog(string content)
        {
            string sql = "insert into log (time, content) values (@time, @content)";
            SQLiteParameter[] parameters = new SQLiteParameter[]
            {
                new SQLiteParameter("@time", DateTime.Now),
                new SQLiteParameter("@content", content)
            };
            DBHelper.ExecuteNonQuery(sql, parameters);
        }
        // 查询所有日志
        public static List<Log> GetAllLogs()
        {
            List<Log> logs = new List<Log>();
            string sql = "select * from log";
            using (SQLiteDataReader reader = DBHelper.ExecuteReader(sql))
            {
                while (reader.Read())
                {
                    int id = reader.GetInt32(0);
                    DateTime time = reader.GetDateTime(1);
                    string content = reader.GetString(2);
                    Log log = new Log(id, time, content);
                    logs.Add(log);
                }
            }
            return logs;
        }

        // 根据编号查询日志
        public static Log GetLogById(int id)
        {
            Log log = null;
            string sql = "select * from log where id = @id";
            SQLiteParameter[] parameters = new SQLiteParameter[]
            {
                new SQLiteParameter("@id", id)
            };
            using (SQLiteDataReader reader = DBHelper.ExecuteReader(sql, parameters))
            {
                if (reader.Read())
                {
                    DateTime time = reader.GetDateTime(1);
                    string content = reader.GetString(2);
                    log = new Log(id, time, content);
                }
            }
            return log;

        }
        // 显示所有日志
        public static void ShowAllLogs()
        {
            List<Log> logs = GetAllLogs();
            Console.WriteLine("所有日志如下：");
            foreach (var log in logs)
            {
                log.ShowInfo();
            }
        }

        // 根据时间范围查询日志
        public static List<Log> GetLogsByTime(DateTime start, DateTime end)
        {
            List<Log> logs = new List<Log>();
            string sql = "select * from log where time between @start and @end";
            SQLiteParameter[] parameters = new SQLiteParameter[]
            {
                new SQLiteParameter("@start", start),
                new SQLiteParameter("@end", end)
            };
            using (SQLiteDataReader reader = DBHelper.ExecuteReader(sql, parameters))
            {
                while (reader.Read())
                {
                    int id = reader.GetInt32(0);
                    DateTime time = reader.GetDateTime(1);
                    string content = reader.GetString(2);
                    Log log = new Log(id, time, content);
                    logs.Add(log);
                }
            }
            return logs;
        }

        // 显示指定时间范围的日志
        public static void ShowLogsByTime(DateTime start, DateTime end)
        {
            List<Log> logs = GetLogsByTime(start, end);
            Console.WriteLine($"从{start}到{end}的日志如下：");
            foreach (var log in logs)
            {
                log.ShowInfo();
            }
        }


    }

}



